//
//  LVRefreshHeader.m
//  Live
//
//  Created by 熙文 张 on 16/5/17.
//  Copyright © 2016年 Heller. All rights reserved.
//

#import "LVRefreshHeader.h"
#import "LVPullToRefreshAnimator.h"
#import "LVLoadingAnimation.h"
#import "LKSequenceAnimation.h"
#import "BNDOfficeDocKit.h"

NSTimeInterval const LKDelayTimeInterval = 1.1f;

@interface LVRefreshHeader()
{
    UIView *_loadingAnimation;
    CAShapeLayer *_lineMaskLayer;
    
    NSTimeInterval _beginRefreshTime; //开始刷新时间
}

@property (strong, nonatomic) LKSequenceAnimation *sequenceAnimation;

//@property (nonatomic, strong) LOTAnimationView  *sequenceAnimation;

@property (strong, nonatomic) UIView       *animationView;
@property (strong, nonatomic) UILabel      *titleLabel;

@property (assign, nonatomic) BOOL isRefreshFinished;
@property (assign, nonatomic) BOOL isDoAnimation;

@end

@implementation LVRefreshHeader

#pragma mark - 重写方法
#pragma mark - 在这里做一些初始化配置（比如添加子控件）
- (void)prepare
{
    [super prepare];
    
    self.mj_h = 68;// 修改

    self.animationView = [[UIView alloc] initWithFrame:CGRectZero];
    [self.animationView setFrame:(CGRect){0, 0, (CGSize){64,20}}];
    [self.animationView setBackgroundColor:[UIColor clearColor]];
    [self.animationView setClipsToBounds:YES];
    [self addSubview:self.animationView];
    
    [self.animationView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.mas_equalTo(self.mas_centerX);
        make.top.mas_equalTo(self.mas_top).offset(10);
        make.size.mas_equalTo((CGSize){30,30});
    }];
    
    _sequenceAnimation = [[LKSequenceAnimation alloc] init];
    [_sequenceAnimation setLineType:LKSequenceAnimationSmall];
    [_sequenceAnimation initImageViewUI:self.animationView];
//    _sequenceAnimation mas
//    [_sequenceAnimation mas_makeConstraints:^(MASConstraintMaker *make) {
//        make.center.mas_equalTo(self);
//        make.size.mas_equalTo(CGSizeMake(60, 60));
//    }];
////
//    _sequenceAnimation = [LOTAnimationView animationNamed:@"loading.json"];
//    [_sequenceAnimation  setLoopAnimation:YES];
//    [self addSubview:_sequenceAnimation];
   

    // 暂时不用
//    UIBezierPath *linePath = [UIBezierPath bezierPathWithRect:(CGRect){0, 0, kDeviceWidth, 40}];
//    _lineMaskLayer = [CAShapeLayer layer];
//    _lineMaskLayer.path = linePath.CGPath;
//    self.animationView.layer.mask = _lineMaskLayer;
    
    _titleLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 100, 18)];
    [_titleLabel setFont:BNDPFRegularFont(11)];
    [_titleLabel setTextColor:UIColorWithHex(0x666666)];
    [_titleLabel setText:@"下拉刷新"];
    [_titleLabel setTextAlignment:NSTextAlignmentCenter];
    [self addSubview:_titleLabel];
    
    [_titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.mas_equalTo(self.mas_right);
        make.left.mas_equalTo(self.mas_left);
        make.bottom.mas_equalTo(self.mas_bottom).offset(-5);
        make.height.mas_equalTo(20);
    }];
    
    [self layoutIfNeeded];
}

#pragma mark - 在这里设置子控件的位置和尺寸

- (void)setState:(MJRefreshState)state
{
    [super setState:state];
    
    switch (state) {
        case MJRefreshStateIdle: {
            if (self.isRefreshFinished) {
                [_titleLabel setText:@"刷新完成"];
            }else {
                [_titleLabel setText:@"下拉刷新"];
            }
            break;
        }
        case MJRefreshStatePulling: {
            [_titleLabel setText:@"松手刷新"];
            break;
        }
        case MJRefreshStateRefreshing: {
            [_titleLabel setText:@"正在刷新"];
            break;
        }
        case MJRefreshStateWillRefresh: {
            break;
        }
        case MJRefreshStateNoMoreData: {
            break;
        }
    }
}

- (void)setLineColor:(UIColor *)lineColor
{
    if (_lineColor != lineColor) {
        _lineColor = nil;
        _lineColor = lineColor;
        [_titleLabel setTextColor:lineColor];
    }
}

- (void)beginRefreshing
{
    [super beginRefreshing];
    
    self.isRefreshFinished = NO;
    
    _beginRefreshTime = CFAbsoluteTimeGetCurrent();
    
    [_sequenceAnimation startAnimation];
}

- (void)endRefreshing
{
    NSTimeInterval delayTime = CFAbsoluteTimeGetCurrent() - _beginRefreshTime;
    if (delayTime < LKDelayTimeInterval) {
        delayTime = LKDelayTimeInterval - delayTime;
    }else {
        delayTime = 0.0;
    }
    
    self.isDoAnimation = YES;
    
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(delayTime * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
        [self endRefreshingWithCompletionBlock:nil];
    });
}

- (void)endRefreshingWithCompletionBlock:(MJRefreshComponentEndRefreshingCompletionBlock)completionBlock
{
    self.isRefreshFinished = YES;

    WS(weakSelf)
    [self setEndRefreshingCompletionBlock:^() {
        
        weakSelf.isDoAnimation = NO;
        weakSelf.isRefreshFinished = NO;
        weakSelf.pullingPercent = 0.0;
        
        [weakSelf.titleLabel setText:@"下拉刷新"];
        
        if (completionBlock) {
            completionBlock();
        }
        
        if (weakSelf.lk_endRefreshingCompletionBlock) {
            weakSelf.lk_endRefreshingCompletionBlock();
        }
    }];
    
    [super endRefreshing];
    [_sequenceAnimation stopAnimation];
}


- (void)startAnimating
{
    [self beginRefreshing];
}

- (void)stopAnimating
{
    [self endRefreshing];
}


//- (void)setPullingPercent:(CGFloat)pullingPercent
//{
//    [super setPullingPercent:pullingPercent];
//
//    if (self.isRefreshing) {
//        return;
//    }
//
//    // 正在做隐藏动画时，不隐藏加载界面
//    if (!self.scrollView.isDragging && self.isDoAnimation) {
//        return;
//    }
//
//    CGFloat percent = MIN(pullingPercent,1.0);
//
//    CGFloat y = 40.0 * (1.0 - percent);
//    CGRect frame = CGRectMake(0, y, kDeviceWidth, 40);
//
//    _lineMaskLayer.frame = frame;
//}

@end
